package club.calong.sanniu.service;
// 团   队: 风讯网络
// 后   端: Dopa
// 开发时间: 2021/7/23 15:15

import club.calong.sanniu.http.request.PlateRequest;
import club.calong.sanniu.http.response.ListResponseData;
import club.calong.sanniu.http.response.PlateResponseData;
import club.calong.sanniu.pojo.Plate;
import club.calong.sanniu.repository.PlateRepository;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PlateService {
    @Resource
    PlateRepository repository;

    public ListResponseData<Plate> list(Integer current, Integer size){
        QueryWrapper<Plate> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc(Plate.ID);
        Page<Plate> page = repository.selectPage(new Page<>(current, size), wrapper);
        return new ListResponseData<>(page.getRecords(), page.getTotal());
    }

    public ListResponseData<Plate> query(Integer current, Integer size, String query, String section){
        QueryWrapper<Plate> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc(Plate.ID);
        if (section != null && section.length() > 0)
            wrapper.eq(Plate.SECTION, section);

        if (query != null)
            wrapper.and(wp -> {
                wp.like(Plate.CONTENT, query).or()
                        .like(Plate.TITLE, query);
            });
        Page<Plate> page = repository.selectPage(new Page<>(current, size), wrapper);
        return new ListResponseData<>(page.getRecords(), page.getTotal());
    }

    public Plate one(Integer id){

        Plate plate = repository.selectById(id);
        return plate;
    }

    public boolean create(Plate plate){

        return repository.insert(plate) == 1;
    }

    public boolean update(Plate plate){

        return repository.updateById(plate) == 1;
    }

    public boolean delete(Integer id){
        return repository.deleteById(id)!=0;
    }
}
